package zuoye.Demo2.util;
import Demo1.DbUtil;
import java.sql.*;
import java.util.Properties;

/**
 * @author yangshuo
 * @since 1.0
 */
public class DBUtil {
    public static String driver;
    public static String url;
    public static String user;
    public static String pass;
    static{
        try {
            Properties p =new Properties();
            p.load(DbUtil.class.getClassLoader().getResourceAsStream("database.properties"));
            driver=p.getProperty("driver");
            url=p.getProperty("url");
            user=p.getProperty("user");
            pass=p.getProperty("password");
            Class.forName(driver);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private DBUtil(){}

    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(url,user,pass);
        } catch (SQLException e) {
            e.printStackTrace();
            throw  new RuntimeException(e);
        }
    }
    public static void close(ResultSet rs,Statement s,Connection con){
        close(rs);
        close(s);
        close(con);
    }
    public static void close(Statement s,Connection con){
        close(s);
        close(con);
    }
    public static void close(ResultSet rs){
        try {
            if(rs!=null &&rs.isClosed()==false)
                rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Statement statement){
        try {
            if(statement!=null &&statement.isClosed()==false)
                statement.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(PreparedStatement ps){
        try {
            if(ps!=null &&ps.isClosed()==false)
                ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void close(Connection connection){
        try {
            if(connection!=null &&connection.isClosed()==false)
                connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
